package com.ogawa.project6602.viewmodel

import androidx.lifecycle.MutableLiveData
import com.ogawa.base.base.BaseViewModel
import com.ogawa.base.bean.LoadState
import com.ogawa.projectcommon.utils.dataConvert
import com.ogawa.projectcommon.utils.launch
import com.ogawa.projectcommon.bean.CollectBean
import com.ogawa.projectcommon.constants.Constant
import com.ogawa.projectcommon.dboperation.OperationDB
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.projectcommon.service.CollectApiService
import com.ogawa.projectcommon.utils.ProjectSpUtils

class CollectViewModel : BaseViewModel() {
    val collectBeans = MutableLiveData<MutableList<CollectBean>>()
    private val api = RetrofitManager.INSTANCE.createService(CollectApiService::class.java)

    fun collect(collectBean: CollectBean) {
        launch({
            collectBean.typeCode = ProjectSpUtils.getCurDeviceTypeCode()
            loadState.value = LoadState.Loading("")
            val userInfo = ProjectSpUtils.getUserInfo()
            if (userInfo.isVisitor) {
                val collects = OperationDB().db.getCollectDao().getCollects()
                if (collects.size == 3) {//收藏已满，移除index为1的收藏，其余收藏index-1
                    collectBean.index = 3
                    val index1 = OperationDB().db.getCollectDao().queryCollectByIndex(1)
                    val index2 = OperationDB().db.getCollectDao().queryCollectByIndex(2)
                    val index3 = OperationDB().db.getCollectDao().queryCollectByIndex(3)
                    OperationDB().db.getCollectDao().deleteCollect(index1)
                    index2.index = index2.index - 1
                    index3.index = index3.index - 1
                    OperationDB().db.getCollectDao().updateCollectInfo(index2)
                    OperationDB().db.getCollectDao().updateCollectInfo(index3)
                } else {
                    collectBean.index = collects.size + 1
                }
                OperationDB().db.getCollectDao().insert(collectBean)
                getCollects()
                loadState.value = LoadState.Success("1")
            } else {
                collectBean.id = -1
                api.collect(collectBean).dataConvert()
                val collects = collectBeans.value
                if (collects?.size == 3) {
                    collects.removeAt(2)
                }
                val bean = CollectBean()
                bean.type = collectBean.type
                bean.programName = collectBean.programName
                bean.program = collectBean.program
                collects?.add(0, bean)
                collectBeans.value = collects
                loadState.value = LoadState.Success("1")
            }
        })
    }

    fun unCollect(collectBean: CollectBean) {
        launch({
            collectBean.typeCode = ProjectSpUtils.getCurDeviceTypeCode()
            loadState.value = LoadState.Loading("")
            val userInfo = ProjectSpUtils.getUserInfo()
            if (userInfo.isVisitor) {
                val collects = OperationDB().db.getCollectDao().getCollects()
                val removeBean = OperationDB().db.getCollectDao()
                    .queryCollectByTypeAndModel(collectBean.type, collectBean.program)
                val index = removeBean.index
                OperationDB().db.getCollectDao().deleteCollect(removeBean)
                if (index < collects.size) {//将要index比要删除的收藏index大的bean-1
                    for (i in index + 1..collects.size) {
                        val bean = OperationDB().db.getCollectDao().queryCollectByIndex(i)
                        bean.index = bean.index - 1
                        OperationDB().db.getCollectDao().updateCollectInfo(bean)
                    }
                }
                getCollects()
                loadState.value = LoadState.Success("2")
            } else {
                api.unCollect(collectBean.type, collectBean.program, ProjectSpUtils.getCurDeviceTypeCode())
                val collects = collectBeans.value
                for (i in collects?.indices!!) {
                    val bean = collects[i]
                    if (bean.type == collectBean.type
                        && bean.program == collectBean.program
                    ) {
                        collects.removeAt(i)
                        break
                    }
                }
                collectBeans.value = collects
                loadState.value = LoadState.Success("2")
            }
        })
    }

    fun getCollects() {
        launch({
            val userInfo = ProjectSpUtils.getUserInfo()
            if (userInfo.isVisitor) {
                val collects = OperationDB().db.getCollectDao().getCollects()
                collects.sortByDescending { it.index }
                collectBeans.value = collects
            } else {
                loadState.value = LoadState.Loading("")
                collectBeans.value = api.getCollects(ProjectSpUtils.getCurDeviceTypeCode()).dataConvert().list
                loadState.value = LoadState.Success("")
            }
        })
    }

    fun getCollectBean(type: Int, modelValue: Int): CollectBean? {
        val userInfo = ProjectSpUtils.getUserInfo()
        return if (userInfo.isVisitor) {
            OperationDB().db.getCollectDao()
                .queryCollectByTypeAndModel(type, modelValue.toString())
        } else {
            collectBeans.value?.forEach { it ->
                if (it.program == modelValue.toString() && type == it.type) {
                    return it
                }
            }
            null
        }
    }
}